#!/usr/bin/perl -w

# Licensed under the GNU Lesser General Public License, version 2.1 (LGPL-2.1)
# Full text is available at:
#
#    http://www.gnu.org/licenses/lgpl-2.1.txt
#
# This file is part of cyg-get.
#
#    cyg-get is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as
#    published by the Free Software Foundation, either version 2.1 of
#    the License, or (at your option) any later version.
#
#    cyg-get is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with cyg-get.
#
#    If not, see <http://www.gnu.org/licenses/>.

use strict;
use warnings;

# Option specified on the command line
my $option = shift;
chomp $option;

# If given an option, and the script for it exists, run it
if( defined($option) and (-x "$0-$option")) {
    my @args;
    
    # Encapsulate all args.  I know there's a better way to do this...
    foreach my $arg (@ARGV) {
        push(@args, "'$arg'");
    }
    
    # Pass control to intended program
    system("$0-$option " . join(' ', @args));
} else {
    $0 =~ m{(.*/)?([^/]+)};
    my $base = $1;
    my $cmd = $2;
    
    my @opts = glob("$base$cmd-*");
    
    if( @opts ) {
        my $useline;
        foreach my $opt (glob("$base$cmd-*")) {
            $opt =~ m{(.*/)?([^/]+)-([^-]+)};
            $useline = `$opt --usage | grep -v 'Description:'`;
            chomp $useline;
            print "\t$3\t$useline";
        }
    }
}

__END__

=head1 NAME

cyg-get - Rudimentary package manager for cygwin

=head1 SYNOPSIS

cyg-get [command] [options]

  command       command to run
  options       options for the specified command

=head1 OPTIONS

=over 8

=item B<--man>

Prints the manual page and exits

=back

=head1 DESCRIPTION

B<This program> manages packages in cygwin.

=cut
